package main;

import Bean.UserInfoBean;
import utils.DBUtils;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.util.*;

public class Open {

    Admin adminScreen;
    User userScreen;
    Scanner sc = new Scanner(System.in);
    static DBUtils dbUtils;
    public void start(){
        System.out.println("---------欢迎进入银行系统---------");
        mainScreen();
    }

    public void mainScreen(){
        first: while(true) {
            System.out.println("请选择你要做什么");
            System.out.println("1 - 登录");
            System.out.println("2 - 注册");
            System.out.println("3 - 退出");
            System.out.println("\n\n");
            switch (sc.nextInt()) {
                case 1:
                    logIn();
                    break first;
                case 2:
                    register();
                    break first;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("不要胡乱输入哦！！！");
                    break;
            }
        }

    }
    public void logIn(){
        System.out.println("请输入用户名：");
        String name = sc.next();
        System.out.println("请输入登录密码：");
        String password = sc.next();

        try {
            // 数据库查询
            String sql = "select * from userinfo where `Del` = 0 and Name = \'" + name +"\' and Password = \'" + password +"\' ";
            UserInfoBean user = dbUtils.selectUser(sql);
            // 判断是否注册
            if(user != null){
                // 已注册 的 判读是否是管理员，进入不同的菜单
                if(user.getPower() == 0){
                    userScreen.mainScreen(user);
                }else{
                    adminScreen.mainScreen(user);
                }
            }else{
                // 未注册的提醒对方注册
                System.out.println("该用户还未注册，是否注册？(y/n)");
                switch(sc.next().charAt(0)){
                    case 'y': register(); break;
                    case 'n':
                        System.out.println("ok！");
                        start();
                        break;
                    default:
                        System.out.println("无效输入");
                }
            }
        }catch (SQLException e){
            System.out.println("数据库没有此用户");
        }
    }
    public void register(){
        UserInfoBean user = new UserInfoBean();
        // 输入用户名
        System.out.println("请输入用户名：");
        String name = sc.next();
        // 输入密码
        System.out.println("请输入登录密码：");
        String pw1 = sc.next();
        // 确认密码
        System.out.println("请确认登录密码：");
        String pw2 = sc.next();

        while(!pw1.equals(pw2)){
            System.out.println("两次密码不同，请重新输入！！！！");
            // 输入密码
            System.out.println("请输入登录密码：");
            pw1 = sc.next();
            // 确认密码
            System.out.println("请确认登录密码：");
            pw2 = sc.next();
        }

        // 输入支付密码
        System.out.println("请输入支付密码：");
        String paywd1 = sc.next();
        // 确认支付密码
        System.out.println("请确认支付密码：");
        String paywd2 = sc.next();

        while(!paywd1.equals(paywd2)) {
            System.out.println("两次密码不同，请重新输入！！！！");
            // 输入支付密码
            System.out.println("请输入支付密码：");
            paywd1 = sc.next();
            // 确认支付密码
            System.out.println("请确认支付密码：");
            paywd2 = sc.next();
        }

        // 验证码
        String verCode = verificationCode(4);
        System.out.println("请输入验证码:[ "+ verCode +" ]");
        String vc = sc.next();

        while(!vc.equals(verCode)){
            System.out.println("验证码不一致，请重新输入！！！！");
            verCode = verificationCode(4);
            System.out.println("请输入验证码:[ "+ verCode +" ]");
            vc = sc.next();
        }

        // 入库，进入主界面
        user.setDelete(0);
        user.setAsset(new BigDecimal(0));
        user.setPower(0);
        user.setName(name);
        user.setPassword(pw1);
        user.setPwd(paywd1);
        user.setBankCard(buildBankCard());

        if(dbUtils.insertToUser(user)){
            // 进入用户界面
            userScreen.mainScreen(user);
        }

    }

    // 银行卡号生成
    public String buildBankCard(){
        long timestamp = System.currentTimeMillis();
        Random random = new Random();
        StringBuilder sb = new StringBuilder(6);
        for (int i = 0; i < 6; i++) {
            sb.append(random.nextInt(10));
        }
        String timestampPart = String.format("%010d", timestamp).substring(6);
        String cardNumber = timestampPart + sb.toString();
        return cardNumber;
    }

    // 验证码生成
    public String verificationCode(int length){
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder captcha = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            captcha.append(characters.charAt(index));
        }

        return captcha.toString();
    }
}
